let LevelScript = require('LevelScript');

cc.Class({
    extends: cc.Component,

    properties: {
        level:{
            default: [],
            type:LevelScript
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        if (Data.account===null) {
            let user = JSON.parse(window.localStorage.getItem('lmwg-account'));
            Data.account = user.account;
            Data.school = user.school;
        }
        if (Data.account && Data.record===null) {
            let url = "https://lemanew.91xbc.com:8101/get_record";
            let xhr = new XMLHttpRequest();
            xhr.responseType = "json";
            xhr.timeout = 3000;
            xhr.onload = ()=> {
                if (xhr.status >= 200 && xhr.status < 300) {
                    try {
                        Data.record = JSON.parse(xhr.response.record);
                    } catch (error) {
                        Data.record = null;
                    }
                }
                if (Data.record===null) {
                    Data.record = [0,0,0,0,0,0,0,0,0,0];
                }
                this.initDir();
            };
            xhr.onerror = ()=> {
                Data.record = [0,0,0,0,0,0,0,0,0,0];
                this.initDir();
            };
            xhr.ontimeout = ()=> {
                xhr.abort();
            };
            xhr.open('POST', url, true);
            xhr.send(JSON.stringify({a:Data.account,s:Data.school,c:"codeorg",l:0}));
        } else {
            this.initDir();
        }
    },
    initDir(){
        for (let i = 0; i < Data.record.length; i++) {
            if (i+1==Data.current) {
                this.level[i].setCurrent();
            }
            else if (Data.record[i]>0) {
                this.level[i].setClear();
            }
        }
    }

    // update (dt) {},
});
